package com.boarsoft.boar.office.group.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.office.entity.OfficeGroupUser;
import com.boarsoft.boar.office.group.OfficeGroupUserBiz;
import com.boarsoft.boar.sys.entity.SysUser;
import com.boarsoft.common.Authorized;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.web.controller.BaseController;

@Component
@RestController
@RequestMapping("/group/user")
public class OfficeGroupUserAction extends BaseController {
	@Autowired
	private OfficeGroupUserBiz groupUserBiz;

	@RequestMapping("/list.do")
	@Authorized(code = "work.group.user.list")
	public ReplyInfo<Object> list(String groupId, String key, String orderBy, int pageNo, int pageSize) {
		PagedResult<OfficeGroupUser> pr = groupUserBiz.list(groupId, key, orderBy, pageNo, pageSize);
		List<SysUser> lt = new ArrayList<SysUser>();
		for (OfficeGroupUser o: pr.getList()) {
			lt.add(this.getSysUser(o.getUserId()));
		}
		return new ReplyInfo<Object>(true, new PagedResult<SysUser>(pr.getTotal(), lt, pageNo, pageSize));
	}

	/**
	 * 为指定角色授权或解除模块权限
	 * 
	 * @return
	 */
	@RequestMapping("/toggle.do")
	@Authorized(code = "work.group.user.toggle")
	public ReplyInfo<Object> toggle(String groupId, String userIds, short status) {
		String[] ua = userIds.split(",");
		groupUserBiz.toggle(groupId, ua, status == 1);
		return ReplyInfo.SUCCESS;
	}
}